
/*
 * .
 * All Rights Reserved.
 */
package com.model.demo.pdf;

import com.model.demo.util.PdfConvertUtil;
import com.model.demo.util.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * PdfConvertController
 *
 * @author Liao Dongshan, 2019年05月08日
 *
 */
@RestController
@RequestMapping("/pdf")
public class PdfConvertController {
        @GetMapping("/test")
        public Result test(){
            return Result.ok();
        }

        @GetMapping("/pdftohtml")
//        @ResponseBody
        public Result pdftohtml(HttpServletRequest request) {
            InputStream in=null;
            try {
                 in=new FileInputStream("D:\\model\\test.PDF");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            PdfConvertUtil pdfConvertUtil = new PdfConvertUtil();
//            String pdfName = file.getOriginalFilename();
//            int lastIndex = pdfName.lastIndexOf(".pdf");
//            String fileName = pdfName.substring(0, lastIndex);
//            String htmlName = fileName + ".html";
            String htmlName="pdftest.html";
//            String realPath = request.getSession().getServletContext().getRealPath("/pdfhtml");
//            String htmlPath = realPath + "\\" + htmlName;
            String htmlPath =  "D:\\pdfhtml\\" + htmlName;
            try {
//                pdfConvertUtil.pdftohtml(file.getBytes(), htmlPath);
                pdfConvertUtil.pdftohtml(in, htmlPath);
                return Result.ok(htmlName);
//                responseInfo.setCode(1);
//                responseInfo.setMessage(htmlName);
            } catch (Exception e) {
//                responseInfo.setMessage("读写文件出现异常，请重新尝试！");
                e.printStackTrace();
            }
            return Result.error("读写文件出现异常，请重新尝试！");
        }

}
